import axios from 'axios'
import { Message } from 'element-ui'

const baseURL = ''

const service = axios.create({
  baseURL,
  timeout: 60000
})

service.interceptors.request.use(
  (config) => {
    // 添加请求头，token之类的
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

service.interceptors.response.use(
  (response) => {
    const result = response.data
    if (result.code === 200) {
      return result
    } else {
      Message({
        message: result.msg || 'error',
        type: 'error',
        duration: 3000
      })
      return Promise.reject(new Error(result.msg || 'error'))
    }
  },
  (error) => {
    Message({
      message: error.msg,
      type: 'error',
      duration: 3000
    })
    return Promise.reject(error)
  }
)

export default service
